﻿using Microsoft.CSharp;
using System;
using System.CodeDom;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEngine;

namespace Simulation.Framework.Editor
{
    /// <summary>
    /// 创建脚本
    /// </summary>
    public class CreateCSWindowBase : EditorWindow
    {
        [SerializeField] protected List<ClassInfo> classInfo = new List<ClassInfo>();
        protected SerializedObject serializedObject;
        protected SerializedProperty classInfosObject;

        // 输出路径
        private string outPath;
        private Vector2 scrollData;
        private void OnEnable()
        {
            if (classInfo != null) classInfo.Clear();
            classInfo = new List<ClassInfo>();

            serializedObject = new SerializedObject(this);
            classInfosObject = serializedObject.FindProperty("classInfo");

            outPath = Application.dataPath;
        }

        protected virtual void OnGUI()
        {
            serializedObject.Update();
            EditorGUI.BeginChangeCheck();

            OutPutPath();
            CreateCS();

            //结束检查是否有修改
            if (EditorGUI.EndChangeCheck())
            {
                //提交修改
                if(serializedObject!= null)
                    serializedObject.ApplyModifiedProperties();
            }
        }

        #region GUI

        private void OutPutPath()
        {
            GUILayout.BeginHorizontal();

            GUILayout.Label("输出路径");
            GUILayout.Label(outPath, GUILayout.Width(350));
            if (GUILayout.Button("选择", GUILayout.Width(60), GUILayout.Height(30)))
            {
                string folder = string.IsNullOrEmpty(outPath) ? Application.dataPath : outPath;
                outPath = EditorUtility.OpenFolderPanel("创建文件", folder, "");
            }

            GUILayout.EndHorizontal();
        }

        private void CreateCS()
        {
            GUILayout.Space(10);
            scrollData = GUILayout.BeginScrollView(scrollData);

            for (int i = 0; i < classInfo.Count; i++)
            {
                GUILayout.BeginHorizontal();

                // 元素
                SerializedProperty element = classInfosObject.GetArrayElementAtIndex(i);
                EditorGUILayout.PropertyField(element, true);

                // 设置显示名称
                SerializedProperty displayNameProperty = element.FindPropertyRelative("displayName");
                displayNameProperty.stringValue = "class";

                if (GUILayout.Button("删除"))
                {
                    classInfo.RemoveAt(i);
                    classInfosObject.DeleteArrayElementAtIndex(i);
                    break;
                }

                GUILayout.EndHorizontal();
            }

            GUILayout.EndScrollView();

            if (GUILayout.Button("添加"))
            {
                int index = classInfo.Count;
                classInfo.Add(new ClassInfo());
                classInfosObject.InsertArrayElementAtIndex(index);
            }

            if (GUILayout.Button("创建"))
            {
                for (int i = 0; i < classInfo.Count; i++)
                {
                    string spaceName = classInfo[i].spaceName;
                    string className = classInfo[i].className;
                    string destFile = outPath + @"/" + className + ".cs";
                    Create(destFile, spaceName, className);
                }

                Clear();
            }
        }

        #endregion

        private void Clear()
        {
            if (classInfosObject != null)
            {
                classInfosObject.Dispose();
                classInfosObject = null;
            }

            if (serializedObject != null)
            {
                serializedObject.Dispose();
                serializedObject = null;
            }
            Close();
        }

        protected  virtual void Create(string destFile, string sName, string className)  { }

        [Serializable]
        public class ClassInfo
        {
            [HideInInspector] public string displayName;
            public string spaceName;
            public string className;
        }
    }
}